define(function(require, exports, module) {
	//define Product model
	//var _ = require('_');
	var _b = require('_b');
	var Product = require('../models/product');
	var Cart = require('../models/cart');
	var ProductList = _b.Collection.extend({		
		  url : HOST+'/front/service/product',
		  model: Product
	});
	var cart = new Cart();
	var ProductItemView = _b.View.extend({
		tagName:'li',
		events:{
			'click .add_cart':'add_cart',
			'click .cart_item_num':'change_cart_item_num'
		},
		change_cart_item_num:function(){
			alert($(this).val());
		},
		add_cart:function(){
			var cartItem = this.model.toJSON();
			cartItem.buy_num = 1;
			cart.add(cartItem);
		},
		template:_.template($('#product-item-template').html()),
		initialize:function(){
			 this.listenTo(this.model,'change',this.render); 
		},
		render:function(){
			this.$el.html(this.template(this.model.attributes));
			return this;
		}
	});
	var ProductListView = _b.View.extend({
		  el: $('#productList'),
		  initialize:function(){
			  this.listenTo(this.collection,'add',this.render); 
			  this.listenTo(this.collection,'remove',this.render); 
			  this.render();
		  },
		  renderItem:function($model,k){
			  var itemview = new ProductItemView({
		            model: $model
		        }); 
			  this.$el.append( itemview.render().el );
		  },
		  render: function() {
			  this.$el.html('');
			  this.collection.each(this.renderItem,this);
			  return this;
		  }

	});
	var CartViewItem = _b.View.extend({
		tagName:'li',
		events:{
			'click .remove_from_cart':'remove_from_cart'
		},
		remove_from_cart:function(){
			this.collection.remove(this.model.id);
		},
		template:_.template($('#cart-item-template').html()),
		initialize:function(){
			 this.listenTo(this.model,'change',this.render); 
		},
		render:function(){
			this.$el.html(this.template(this.model.attributes));
			return this;
		}
	});
	var CartView = _b.View.extend({
		  el: $('#cartList'),
		  initialize:function(){
			  this.listenTo(this.collection,'add',this.render); 
			  this.listenTo(this.collection,'remove',this.render); 
			  this.render();
		  },
		  renderItem:function($model,k){
			  var itemview = new CartViewItem({
		            model: $model,
		            collection:this.collection
		        }); 
			  this.$el.append( itemview.render().el );
		  },
		  render: function() {
			  this.$el.html('');
			  this.collection.each(this.renderItem,this);
			  return this;
		  }

	});
	
	
	//product list
	var list = new ProductList();
	var productListView = new ProductListView({
		collection:list
	});
	var cartView = new CartView({
		collection:cart
	});
	list.fetch();
	//productListView.render();
	//list.on( 'reset', productListView.render, productListView );
	
	require('../models/cart');
	module.exports = {
			product_list:list,
			cart:cart,
			'productListView':productListView
	};
	
	
});